<!--  -->
<script setup>

import moment from 'moment'
import PopWindow from '@/components/PopWindow.vue'
import { useBreadsStore } from '@/stores'
import { onMounted, ref } from 'vue'
import { getNewsListAPI } from '@/apis/news'
const { setBreadCrumbs } = useBreadsStore()

setBreadCrumbs([
  { title: 'News', path: '/news' }
])

const newsList = ref([])
const condition = ref(false)
const activeNews = ref({})

onMounted(() => {
  getNewsListAPI().then(res => {
    newsList.value = res.data
  })
})

function openNewsDetail(news) {
  activeNews.value = news
  condition.value = true
}

function closeNewsDetail() {
  condition.value = false
  activeNews.value = {}
}
</script>


<template>
  <div class="view-wrapper">
    <div class="view-header">
      <h1 class="view-title">News</h1>
    </div>
    <div class="view-content">
      <div class="section-content">
        <div class="table">
          <div class="log" v-for="(news, idx) in newsList" :key="news.id">
            <div class="count">{{ newsList.length - idx }}</div>
            <div class="date">{{ moment(news.createAt).format('YYYY-MM-DD') }}</div>
            <div class="item" @click="openNewsDetail(news)">
              {{ news.title }} {{ news.content ? '..' : '' }}
            </div>
          </div>
        </div>
      </div>
      <div class="section-content">
        <div class="news-content">
        </div>
      </div>
    </div>
    <div class="view-footer">
    </div>
    <PopWindow :condition="condition" @close="closeNewsDetail">
      <div class="pop-window-header">
        <div class="title">News Detail</div>
      </div>
      <div class="pop-window-body">
        <div class="label">Public date<span class="desc"></span></div>
        <p>{{ moment(activeNews.createTime).format('YYYY-MM-DD HH:mm:ss') }}</p>
        <div class="label">Topic<span class="desc"></span></div>
        <p>{{ activeNews.title }}</p>
        <div class="label">Content<span class="desc"></span></div>
        <p>{{ activeNews.content }}</p>
      </div>
      <div class="pop-window-footer">
      </div>
    </PopWindow>
  </div>
</template>

<style scoped lang="scss">
.view-wrapper {
  margin: 50px auto 30px;
  flex: 1;
  width: 60%;
  background-color: var(--site-home-bgc);
  box-shadow: 0px 3px 4px var(--site-home-shadow);
  border: 1px solid var(--border-primary);
  border-radius: 2px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  @media screen and (max-width: 768px) {
    width: 95%;
  }

  @media screen and (min-width: 769px) and (max-width: 882px) {
    width: 80%;
  }

  @media screen and (min-width: 883px) and (max-width: 992px) {
    width: 70%;
  }
}

.view-header {
  margin: 10px 0 20px;
  padding-bottom: 10px;
  background: rgba(183, 225, 164, 0.768);
  box-shadow: 0px 4px 4px rgba(113, 123, 109, 0.73);
}

.view-title {
  padding-top: 10px;
  padding-bottom: 0px;
  font-family: "Oswald", "Bookman Old Style", serif;
  font-size: 2.50rem;
  border-bottom: 1px solid black;
  letter-spacing: 0.12rem;
  width: max-content;
  margin: auto;
  color: rgb(1, 71, 3);
}

.view-content {
  min-height: 250px;
}

.table {
  border: 15px ridge rgba(205, 203, 162, 0.285);
  margin: 30px auto;
  width: 90%;
  padding: 0px;

  .log {
    display: grid;
    grid-template-columns: minmax(4%, max-content) minmax(14%, max-content) 1fr;
    gap: 1%;
    width: 100%;
    padding: 0 10px;
    font-size: 0.850rem;
    background: var(--bgc01);

    &:nth-child(even) {
      background: rgba(148, 148, 148, 0.165);
    }


    .count {
      display: block;
      padding: 2px;
      color: var(--text-grey);
      align-self: center;
    }

    .date {
      font-family: Consolas, Menlo, Monaco, Liberation Mono, Lucida Console, monospace;
      display: block;
      padding: 2px;
      color: rgb(64, 68, 69);
      background: rgb(178, 196, 174);
      align-self: center;
      text-align: center;
    }

    .item {
      display: block;
      padding: 2px;
      color: var(--text-grey);
      align-self: center;
      cursor: pointer;

      &:hover {
        color: red;
      }
    }
  }
}

.section-title {
  width: 25%;
  padding-right: 10px;
  margin: 10px 0 10px auto;
  min-height: 15px;
  text-align: right;
  font-weight: bold;
  color: rgb(2, 94, 94);
  border: 1px solid #5f866f86;
  background-color: #add5c984;
}

.divider {
  width: 40%;
  margin: 8px auto;
  border: 0;
  color: #73827a;
  background: linear-gradient(currentColor, currentColor) no-repeat center;
  background-size: calc(100% - 1.5em - 6px) 1px;
  display: flex;
  justify-content: space-between;

  &::before,
  &::after {
    content: '';
    background-color: #5f866f86;
    display: block;
    width: .75em;
    height: .75em;
    transform: rotate(45deg);
    box-sizing: border-box;
    border: 1px solid;
    margin: 3px;
  }
}

.view-footer {
  text-align: center;
  border-top: 1px dotted var(--border-primary);
  font-family: "Oswald", "Bookman Old Style", serif;
  font-size: 1.25rem;
  letter-spacing: 0.12rem;
  background: rgba(183, 225, 164, 0.359);
}

.pop-window-header {

  .title {
    font-size: 1.5rem;
    font-weight: 600;
    line-height: 2rem;
    color: var(--text-03);
  }
}

.pop-window-body {
  flex: 1;
  margin: 2em 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;

  .label {
    font-weight: 400;
    color: var(--text-02);

    .desc {
      margin-left: 10px;
      font-size: 0.8rem;
      color: #979797;
    }
  }

  p {
    border-left: 2px solid var(--text-02);
    padding-left: 1em;
    
  }

  .pop-window-footer {
    display: flex;
    justify-content: end;
  }
}
</style>
